So sometimes, we can actually omit the else clause if we don't need it.
We can just have an if with no accompanying else.
So, in this code, we first assign the variable X to 10, and then we say,
if X is less than zero, so in other words,
if X is negative,
then we print this out.
Note here, we don't have an else after this if,
instead we just have a print statement.
That as the contents imply, always gets printed.
So, when we run this code,
because X is 10,
we should expect the expression X less than zero to
be false and so we should expect only,
this is always printed to printout.
If we run our code, that's the case.
Now, what if we changed X to be negative 10?
Rather than running this code,
I'm just going to go to this multiple choice question which has that exact code.
So, we first have X equals negative 10.
Then we say, if X is less than zero,
then printout the negative number.
X is not valid here,
then we printout this is always printed.
So, we should expect that because negative 10 is less than zero,
that this if clause is going to execute,
so it should printout the negative number,
negative 10 is not valid here.
Then, this is always printed because it's outside of the if,
should be printed as well.
So, our answer should be B.
In this question, we have similar code.
So we say X is negative 10.
If X is less than zero,
then printout the negative number.
X is not valid here.
But then we add in else saying,
otherwise print X is a positive number.
But then we have another else that prints out this is always printed.
The question is, will the following code cause an error?
The answer is going to be yes.
The reason is that every else has to have an accompanying if, right?
So, you never would start off a conversation or a sentence by starting with otherwise.
If you do that,
then it's like well, otherwise what?
You have to have a thing that you're saying otherwise in response to.
So, every else always has to have an accompanying if.
So, this else corresponds with this if.
This else has no if that it corresponds with.
So, Python says, I don't understand what we're actually
writing here and so it's going to give us a syntax error.
So yes, this code causes an error.
One thing to note with conditionals is that inside of an if block or an else block,
we can also have another set of conditionals.
So remember that whatever goes inside of a block, so for example,
inside of this else block is all indented
and only runs if what's inside of this if is false.
But, we can have other if and else statements inside of either these blocks.
So for example, here,
inside of this else block,
we have another if statement saying if X is greater than Y, then do something.
Otherwise in this else corresponds with this if.
So, otherwise print X and Y must be equal.
So, these are what are called nested conditionals.
You can have any level of nesting.
So for example, this if block could have another if else statement,
which could have another if else statement as many times as we want it to.
So, when we look at this code,
we first assign X and Y to 10,
on lines one and two.
Then we say, if X is less than Y,
so 10 is not less than 10.
So, this statement is false.
What will happen is we'll jump to the else.
Then we say, if X is greater than Y,
but 10 is not greater than 10.
So, this is also false.
So, we then jump to this else,
and what gets printed out is X and Y must be equal.
So, let's run our code just to verify that this is the case.
So, we get X and Y must be equal.
Let's change our values for X and Y a little bit.
So, let's start by changing X2 instead be five.
So, when we change X to five,
then this if X is less than Y,
so five is less than 10.
That's true and so we printout X is less than Y,
and we skip this entire else block.
So, the only thing that gets printed out is X is less than Y.
Okay? So, now let's change our numbers slightly again.
So, I'm going to say X is 10 again,
and I'm going to change Y to be three.
So now, 10 less than three, this is false.
So, we skip to the else, and then we say,
if X is greater than Y,
10 greater than three is true.
So, we run what's inside of this nested if,
and we printout X is greater than Y.
So, we're going to come back to this code and learn
one new construct of if and else statements called an elif.
But before then, let's answer some questions.
So, in this code, we have another nested conditional
because this else has an if else inside of it.
The question we're asking is,
will this code cause an error?
Well, I don't see anything invalid in
this code because this else is allowed to have this if else in it.
So I'm going to say, no,
this code is not going to cause an error.
So, let's actually figure out what this code is going to output.
So, we assign X to be negative 10 first, and then we say,
if X is less than zero,
printout the negative number,
negative 10 is not valid here.
Because this if ran,
then we entirely skip this else.
So, the only thing that would get printed is the negative number,
negative 10 is not valid here.
Now, recall this code from the nested conditionals discussion.
So here, we assign two variables,
X and Y to be numbers.
If X is less than Y,
we want to printout X is less than Y.
If X is greater than Y,
we want to printout X is greater than Y,
and if they're equal, we want to printout X and Y must be equal.
So far, we've needed to do this through nested conditionals.
So, in other words,
this else statement had to contain another if statement and else statement.
So, in other words,
if X is less than Y,
we printout X is less than Y.
If X is not less than Y,
but it is instead greater than Y,
then we printout X is greater than Y.
If X is not less than Y,
and X is not greater than Y,
then we printout that they must be equal.
But Python offers a cleaner way to express conditionals like this.
So, in this case,
rather than having an if and else contained within this else,
we can write something called an elif.
So, an elif is short for else if.
So, elif looks exactly like this.
So now, we have the exact same logic that we had before,
but it's a little flatter and doesn't require nesting.
So, we say, if X is less than Y,
then printout X is less than Y, just like before.
But then after that,
we write elif, which again is short for else if.
So in other words, if X is not less than Y,
but X is greater than Y,
then we print this out.
Now, this also saying,
if none of the conditions in this elif or this if were true,
then run what's in here.
So, when I run this code,
when X and Y are 10,
then I'm going to printout X and Y must be equal.
So, just like "else",
every elif has to have an accompanying "if".
So, this elif corresponds to this "if",
and this "else" corresponds to this "if" as well.
Remember, you don't start off a sentence or a conversation by saying otherwise.
Likewise, you aren't going to start off an "if conditional" by saying elif,
you always have to start with an "if".
So, here's a diagram that explains where you use if, elif, and else.
So, every conditional group always starts out with exactly one "if".
So, we have "if" and then some Boolean expression and
then some code to run if that Boolean expression is true.
Then, we can have any number of elifs.
In other words, we can have zero or more elifs that come right after that "if".
So, if we say elif condition two,
then this block runs,
"if condition two is true and nothing earlier in this 'if' group run," so in other words,
condition one was not true.
This elif condition three runs if condition three is true,
and condition one and condition two were not true, and so on.
So, at condition x,
then this runs if condition x is true and none of these other ifs or elifs were true.
So then, after the "if" in any number of elifs,
we can have either zero or one "else" statements.
In other words, we can omit this "else" statement or we can have one of them.
This "else" statement is going to run only if none of
these conditions inside of this if block were true.
So, in other words, this is saying,
"If something's true, otherwise,
if that thing was false and this thing is true, otherwise,
if everything else so far was false and this is true,
and then the 'else' is saying, otherwise,
if everything so far was false, then run this."
So, for example, here are some valid if, elif, else orders.
So, we can have an "if" with nothing else,
we can have an "if" with one "else" statement,
we can have an "if" that has one elif and and an "else",
and we can have an "if" with two or any number of elifs with no "else".
So, let's answer some questions about if, elif, and else.
So, here we have some code and the question asks,
"Which one of one, two,
or three gives the same result as the following nested 'if'".
So, in other words, what we're doing is we're trying to replicate this code within elif.
So, what I would do is I would say, "Okay,
If we join this 'if' and 'else' into an elif,
so I would say if x is less than zero,
elif x greater than zero, and then 'else'".
So, this code is not valid because we have "else" and then a conditional,
but whenever we write "else" we can't have a conditional,
so we know that the answer isn't one,
and in fact, one would cause a syntax error.
For answer two, we have pretty much exactly what we wrote here.
So, we joined this "else" and "if" and made this "else" a clause on this "if".
So, I'm going to say that the answer is answer two.
But let's look at answer three just in case.
So, we have an "If x is less than zero,
then print out the negative number x is not valid here",
and then we have another "if".
So, in other words,
we have one "if" clause that goes from here here,
and another "if" group that goes from here to here,
then includes an "else".
Now, this code looks like it's going to be exactly the same,
but it's actually a little bit different.
So, I'm going to answer.
Number two is the answer here.
We can tell that that's the right answer.
So, this question asks,
"What will the following code print if x is equal to three,
y is equal to five,
and z is equal to two?"
So here, we have a slightly more complex Boolean expression to say,
"if x is less than y and x is less than t." So first,
is three less than five?
Yes, that's true. Then we ask,
"Is three less than two?"
That's false. So, true and false is false,
so this "if" does not execute.
If y is less than x,
so five is not less than three.
So, we know that this is going to be false.
We don't even need to know whether this is true or false
because we know that false and any other value is going to be false.
So, what that means is that we run what's inside of the "else" clause,
so we print out C. In this question,
we're asked to write code using one conditional
to find whether the string false is in the string str1,
which is specified here.
So, I'm going to first write out the Boolean expression to express that.
So, that's the string false in str1.
In the question, we say,
"If that's true, then assign the variable output to this string."
So, I'm going to say,
"If this is true,
then output equals the string 'False.
You aren't you?'" Then it asks,
"Check to see if the string 'true' is in str1,
and if that is, then assign 'True.
You are you' to the variable output."
So, I'm going to put that in an elif.
So elif true is in str1, output equals "True.
You are you."
If neither are in str1,
then assign this string to the variable output.
So, we're going to express that with an "else".
Else, remember else doesn't take a condition because its condition
is implicitly that neither of these conditions were true.
So, we say, "'else' output equals neither true nor false."
I'll leave it up to you to complete the remaining exercises.